<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    top="5vh"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="130px"
    >
      <el-form-item label="唯一标识" prop="openid">
        <el-input v-model="dataForm.openid" placeholder="唯一标识"></el-input>
      </el-form-item>
      <el-form-item label="管理员权限" prop="admin">
        <dict
          defaultMsg="管理员权限"
          v-model="dataForm.admin"
          :selectDict.sync="dataForm.admin"
          dictType="user_role"
          :isRadio="true"
        />
      </el-form-item>
      <el-form-item label="用户状态" prop="state">
        <dict
          defaultMsg="用户状态"
          v-model="dataForm.state"
          :selectDict.sync="dataForm.state"
          dictType="status"
          :isRadio="true"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="nickName">
        <el-input v-model="dataForm.nickName" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="avatarUrl">
        <el-input v-model="dataForm.avatarUrl" placeholder="头像"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <dict
          defaultMsg="性别"
          v-model="dataForm.gender"
          :selectDict.sync="dataForm.gender"
          dictType="sex"
          :isRadio="true"
        />
      </el-form-item>
      <el-form-item label="登录地址" prop="province">
        <el-input v-model="dataForm.province" placeholder="登录地址"></el-input>
      </el-form-item>
      <el-form-item label="是否已经同步资料" prop="sync">
        <dict
          defaultMsg="管理员权限"
          v-model="dataForm.sync"
          :selectDict.sync="dataForm.sync"
          dictType="yes_or_no"
          :isRadio="true"
        />
      </el-form-item>
      <el-form-item label="创建个数" prop="foundNum">
        <el-input v-model="dataForm.foundNum" placeholder="创建个数"></el-input>
      </el-form-item>
      <el-form-item label="最大创建个数" prop="maxFoundNum">
        <el-input
          v-model="dataForm.maxFoundNum"
          placeholder="最大创建个数"
        ></el-input>
      </el-form-item>
      <el-form-item label="参加个数" prop="joinNum">
        <el-input v-model="dataForm.joinNum" placeholder="参加个数"></el-input>
      </el-form-item>
      <el-form-item label="最大参加个数" prop="maxJoinNum">
        <el-input
          v-model="dataForm.maxJoinNum"
          placeholder="最大参加个数"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import dict from "@/components/dictionary/selectPanel";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        openid: "",
        admin: "",
        state: "",
        nickName: "",
        avatarUrl: "",
        gender: "",
        province: "",
        sync: "",
        foundNum: "",
        maxFoundNum: "",
        joinNum: "",
        maxJoinNum: "",
        deleteTag: ""
      },
      dataRule: {}
    };
  },
  components: { dict },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/numcount/wechatuser/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.dataForm.openid = data.data.openid;
              this.dataForm.admin = data.data.admin;
              this.dataForm.state = data.data.state;
              this.dataForm.nickName = data.data.nickName;
              this.dataForm.avatarUrl = data.data.avatarUrl;
              this.dataForm.gender = data.data.gender;
              this.dataForm.province = data.data.province;
              this.dataForm.sync = data.data.sync;
              this.dataForm.foundNum = data.data.foundNum;
              this.dataForm.maxFoundNum = data.data.maxFoundNum;
              this.dataForm.joinNum = data.data.joinNum;
              this.dataForm.maxJoinNum = data.data.maxJoinNum;
              this.dataForm.deleteTag = data.data.deleteTag;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/numcount/wechatuser/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: `${!this.dataForm.id ? "post" : "put"}`,
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              openid: this.dataForm.openid,
              admin: this.dataForm.admin,
              state: this.dataForm.state,
              nickName: this.dataForm.nickName,
              avatarUrl: this.dataForm.avatarUrl,
              gender: this.dataForm.gender,
              province: this.dataForm.province,
              sync: this.dataForm.sync,
              foundNum: this.dataForm.foundNum,
              maxFoundNum: this.dataForm.maxFoundNum,
              joinNum: this.dataForm.joinNum,
              maxJoinNum: this.dataForm.maxJoinNum,
              deleteTag: this.dataForm.deleteTag
            })
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  },
  mounted() {}
};
</script>

<style>
.el-input {
  width: 70%;
}
.el-form-item__label {
  padding-right: 10px;
  width: 120px;
}

.el-dialog {
  overflow-y: scroll;
  height: 650px;
}
.el-dialog__wrapper {
  overflow-y: hidden;
}
.el-dialog__header {
  /* width: 743px; */
  width: 50%;
  /* display: block; */
  position: fixed;
  display: flex;
  justify-content: space-between;
  background-color: rgb(23, 179, 163);
  z-index: 9999;
}
.el-dialog__title {
  color: #fff;
}
.el-dialog__headerbtn .el-dialog__close {
  color: #fff;
}
.el-dialog__headerbtn {
  position: static;
}
.el-dialog__body {
  margin-top: 55px;
}
.el-dialog__footer {
  /* position: fixed; */
  z-index: 9999;
}
</style>
